import { PLAY_MODE } from '@/assets/js/constant';
import { shuffle } from '@/assets/js/util';

export function selectplay({ commit }, { list, index }) {
  // 设置播放模式
  commit('setPlayMode', PLAY_MODE.sequence);
  // 设置顺序播放列表
  commit('setSequenceList', list);
  // 修改播放状态
  commit('setPlayingState', true);
  // 设置播放器状态(点击歌曲时需要展开)
  commit('setFullScreen', true);
  // 设置播放列表
  commit('setPlayList', list);
  // 设置当前播放歌曲索引
  commit('setCurrentIndex', index);
}

// 随机播放列表
export function randomPlay({ commit }, list) {
  // 设置播放模式
  commit('setPlayMode', PLAY_MODE.random);
  // 设置顺序播放列表
  commit('setSequenceList', list);
  // 修改播放状态
  commit('setPlayingState', true);
  // 设置播放器状态(点击歌曲时需要展开)
  commit('setFullScreen', true);
  // 设置播放列表(重新洗牌)
  commit('setPlayList', shuffle(list));
  // 设置当前播放歌曲索引(随机列表生成之后播放第一首歌曲)
  commit('setCurrentIndex', 0);
}

// 模式切换
export function changeMode({ commit, state, getters }, mode) {
  // 获取当前正在播放的歌曲id
  const currentId = getters.currentSong.id;
  // random需要修改播放列表playList
  if (mode === PLAY_MODE.random) {
    // 将顺序列表打乱
    commit('setPlayList', shuffle(state.sequenceList));
  } else {
    // 顺序播放或循环播放,不洗牌,直接返回列表
    commit('setPlayList', state.sequenceList);
  }
  // 找到新列表中当前正在播放的歌曲id
  const index = state.playList.findIndex((song) => {
    return song.id === currentId;
  });
  // 将新列表中当前歌曲的id重新赋值给当前播放歌曲id,可以使在用户切换至随机播放的时候不会切歌
  commit('setCurrentIndex', index);
  // 修改mode
  commit('setPlayMode', mode);
}
// 测试commit
export function removeSong({ commit, state }, song) {
  const sequenceList = state.sequenceList.slice();
  const playlist = state.playList.slice();
  const sequenceIndex = findIndex(sequenceList, song);
  const playIndex = findIndex(playlist, song);
  // playIndex < 0说明在列表里没有知找到这首歌
  if (sequenceIndex < 0 || playIndex < 0) {
    return;
  }

  sequenceList.splice(sequenceIndex, 1);
  playlist.splice(playIndex, 1);

  // 获取正在播放歌曲下标
  let currentIndex = state.currentIndex;
  // bug1: 在删除当前歌曲前一首歌时,当前播放歌曲的索引会发生变化,需要手动修改currentIndex
  // 删除当前播放歌曲前面的歌,需要将当前播放歌曲索引往前移一位
  // bug2: 当前播放歌曲是最后一首时删除最后一首歌会报错,假设播放列表数组长度为10,删除最后一首歌数组长度为9,当前currentIndex还没有变化,就相当于当前播放歌曲下标还是9,会造成数组溢出
  if (playIndex < currentIndex || currentIndex === playlist.length) {
    currentIndex--;
  }

  commit('setSequenceList', sequenceList);
  commit('setPlayList', playlist);
  commit('setCurrentIndex', currentIndex);
  // 如果删除全部歌曲playlist为空时,需要暂停歌曲
  if (!playlist.length) {
    // 停止播放歌曲
    commit('setPlayingState', false);
  }
}

// 清空播放列表
export function clearSongList({ commit }) {
  commit('setSequenceList', []);
  commit('setPlayList', []);
  commit('setCurrentIndex', 0);
  commit('setPlayingState', false);
}

export function addSong({ commit, state }, song) {
  // 往playlist和sequenceList中添加一首歌
  const playlist = state.playList.slice();
  const sequenceList = state.sequenceList.slice();
  // 如果列表中已存在选中歌曲只需要改变currentIndex即可
  let currentIndex = state.currentIndex;
  const playIndex = findIndex(playlist, song);

  if (playIndex > -1) {
    currentIndex = playIndex;
  } else {
    playlist.push(song);
    currentIndex = playlist.length - 1;
  }

  const sequenceIndex = findIndex(sequenceList, song);
  if (sequenceIndex === -1) {
    sequenceList.push(song);
  }
  commit('setSequenceList', sequenceList);
  commit('setPlayList', playlist);
  commit('setCurrentIndex', currentIndex);
  commit('setPlayingState', true);
  commit('setFullScreen', true);
}

// 获取当前播放歌曲索引通用方法
function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.id === song.id;
  });
}
